<?php

/**
 * 统计概况-时段分析
 *
 * @author haifeng.wu
 */

require_once 'CommonController.php';

class Admin_TimeBucketAnalyticsController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_HoursData();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function timeanalyticsAction()
    {
        $vars = array();
        $wheres = array();
        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        if (!empty($vars['osName'])) {
            $wheres[] = "`osName` = '{$vars['osName']}'";
        }
        
        if (empty($vars['startTime'])) {
            $vars['startTime'] = date('Y-m-d',strtotime('-1 day'));
        }
        if (empty($vars['endTime'])) {
            $vars['endTime'] = date('Y-m-d',strtotime('-1 day'));
        }
        
        if(strtotime($vars['startTime']) > strtotime($vars['endTime'])){
            echo $this->view->message('请正确选择时间') ;
            exit;
        }
        $timec = strtotime($vars['startTime']);
        $timee = strtotime($vars['endTime']);
        $datestartNow = $vars['startTime'];
        $dateendNow = $vars['endTime'];
        $datestartOne = date('Y-m-d', ($timec-24*60*60));
        $dateendOne = date('Y-m-d', ($timee-24*60*60));
        $datestartSeven = date('Y-m-d', ($timec-24*60*60*7));
        $dateendSeven = date('Y-m-d', ($timee-24*60*60*7));
        $datestartThirty = date('Y-m-d', ($timec-24*60*60*30));
        $dateendThirty = date('Y-m-d', ($timee-24*60*60*30));
        $wheres[] = "(
                        (`date` >= '{$datestartNow}' AND `date` <= '{$dateendNow}') 
                        OR (`date` >= '{$datestartOne}' AND `date` <= '{$dateendOne}') 
                        OR (`date` >= '{$datestartSeven}' AND `date` <= '{$dateendSeven}') 
                        OR (`date` >= '{$datestartThirty}' AND `date` <= '{$dateendThirty}')
                     )";
        $productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $partnerProductModel = new Application_Model_PartnerProduct();
        $vars['productName'] = $partnerProductModel->getProductNameByProductCode($productCode);
        $wheres[] = "`productCode` = '{$productCode}'";
        $where = '';
        $where = empty($wheres) ? '' : ' WHERE '.implode(' AND ', $wheres) ;
        $sql = "SELECT productCode,osName,`date`,`hours`,sum(bootNum) AS bootNum,sum(bootClientNum) AS bootClientNum,sum(okOrderNum) AS okOrderNum,sum(okOrderAmount) AS okOrderAmount  
                FROM " . $this->_model->getTableName() . $where . " 
                GROUP BY `date`,hours 
                ORDER BY `date`,hours ASC
                ";
        $rows = $this->_model->getAdapter()->fetchAll($sql);
        $datat = array('today'=>array(),'yestoday'=>array(),'seven'=>array(),'thirty'=>array());
        foreach ($rows as $item) {
            if ($item['date'] >= $datestartNow && $item['date'] <= $dateendNow) {
                $datat['today'][] = $item;
            }
            if ($item['date'] >= $datestartOne && $item['date'] <= $dateendOne) {
                $datat['yestoday'][] = $item;
            }
            if ($item['date'] >= $datestartSeven && $item['date'] <= $dateendSeven) {
                $datat['seven'][] = $item;
            }
            if ($item['date'] >= $datestartThirty && $item['date'] <= $dateendThirty) {
                $datat['thirty'][] = $item;
            }
        }
        foreach ($datat as $k => $v) {
            $arrTmpdaynum = array();
            foreach ($v as $key => $val) {
                $arrTmpdaynum[$val['date']] = $val['date'];
            }
            $tmpnumc = (count($arrTmpdaynum) == 0) ? 1 : count($arrTmpdaynum);
            if($k == 'today') {
                $todayNumc = $tmpnumc;
            }
            if($k == 'yestoday') {
                $yestodayNumc = $tmpnumc;
            }
            if($k == 'seven') {
                $sevenNumc = $tmpnumc;
            }
            if($k == 'thirty') {
                $thirtyNumc = $tmpnumc;
            }
        }
        $arrTm = array('today'=>array(),'yestoday'=>array(),'seven'=>array(),'thirty'=>array());
        foreach ($datat as $key => $val) {
            foreach ($val as $item) {
                $arrTm[$key][$item['hours']]['productCode'] = $item['productCode'];
                $arrTm[$key][$item['hours']]['osName'] = $item['osName'];
                $arrTm[$key][$item['hours']]['date'] = $item['date'];
                $arrTm[$key][$item['hours']]['hours'] = $item['hours'];
                $arrTm[$key][$item['hours']]['bootNum'] = isset($arrTm[$key][$item['hours']]['bootNum']) ? ($item['bootNum']+$arrTm[$key][$item['hours']]['bootNum']) : $item['bootNum'];
                $arrTm[$key][$item['hours']]['bootClientNum'] = isset($arrTm[$key][$item['hours']]['bootClientNum']) ? ($item['bootClientNum']+$arrTm[$key][$item['hours']]['bootClientNum']) : $item['bootClientNum'];
                $arrTm[$key][$item['hours']]['okOrderNum'] = isset($arrTm[$key][$item['hours']]['okOrderNum']) ? ($item['okOrderNum']+$arrTm[$key][$item['hours']]['okOrderNum']) : $item['okOrderNum'];
                $arrTm[$key][$item['hours']]['okOrderAmount'] = isset($arrTm[$key][$item['hours']]['okOrderAmount']) ? ($item['okOrderAmount']+$arrTm[$key][$item['hours']]['okOrderAmount']) : $item['okOrderAmount'];
            }
        }
//        foreach ($arrTm as $key => $val) {
//            if($key == 'today') {
//                $daynum = $todayNumc;
//            } else if($key == 'yestoday') {
//                $daynum = $yestodayNumc;
//            } else if($key == 'seven') {
//                $daynum = $sevenNumc;
//            } else {
//                $daynum = $thirtyNumc;
//            }
//            foreach ($val as $item) {
//                $arrTm[$key][$item['hours']]['bootNum'] = round($arrTm[$key][$item['hours']]['bootNum']/$daynum);
//                $arrTm[$key][$item['hours']]['bootClientNum'] = round($arrTm[$key][$item['hours']]['bootClientNum']/$daynum);
//                $arrTm[$key][$item['hours']]['okOrderNum'] = round($arrTm[$key][$item['hours']]['okOrderNum']/$daynum);
//                $arrTm[$key][$item['hours']]['okOrderAmount'] = round($arrTm[$key][$item['hours']]['okOrderAmount']/$daynum, 3);
//            }
//        }
        foreach ($arrTm as $key => $val) {
            for($i=0; $i <= 23; $i++) {
                if (!isset($arrTm[$key][$i])) {
                    $arrTm[$key][$i] = array(
                        'hours' => $i,
                        'bootNum' => 0,
                        'bootClientNum' => 0,
                        'okOrderNum' => 0,
                        'okOrderAmount' => 0
                    );
                }
            }
        }
        
        $todayData = array();
        $yestodayData = array();
        $sevenData = array();
        $thirtyData = array();
        foreach ($arrTm as $key => $val) {
            if ($key == 'today') {
                $todayBootClientNumArr = array();
                $todayBootNumArr = array();
                $todayOkOrderNumArr = array();
                $todayOkOrderAmountArr = array();
                for($i=0; $i <= 23; $i++) {
                    $todayBootClientNumArr[] = $val[$i]['bootClientNum'];
                    $todayBootNumArr[] = $val[$i]['bootNum'];
                    $todayOkOrderNumArr[] = $val[$i]['okOrderNum'];
                    $todayOkOrderAmountArr[] = $val[$i]['okOrderAmount'];
                }
                
                $todayData['bootClientNum'] = implode(', ', $todayBootClientNumArr);
                $todayData['bootNum'] = implode(', ', $todayBootNumArr);
                $todayData['okOrderNum'] = implode(', ', $todayOkOrderNumArr);
                $todayData['okOrderAmount'] = implode(', ', $todayOkOrderAmountArr);
            }
            if ($key == 'yestoday') {
                $yestodayBootClientNumArr = array();
                $yestodayBootNumArr = array();
                $yestodayOkOrderNumArr = array();
                $yestodayOkOrderAmountArr = array();
                for($i=0; $i <= 23; $i++) {
                    $yestodayBootClientNumArr[] = $val[$i]['bootClientNum'];
                    $yestodayBootNumArr[] = $val[$i]['bootNum'];
                    $yestodayOkOrderNumArr[] = $val[$i]['okOrderNum'];
                    $yestodayOkOrderAmountArr[] = $val[$i]['okOrderAmount'];
                }
                
                $yestodayData['bootClientNum'] = implode(', ', $yestodayBootClientNumArr);
                $yestodayData['bootNum'] = implode(', ', $yestodayBootNumArr);
                $yestodayData['okOrderNum'] = implode(', ', $yestodayOkOrderNumArr);
                $yestodayData['okOrderAmount'] = implode(', ', $yestodayOkOrderAmountArr);
            }
            if ($key == 'seven') {
                $sevenBootClientNumArr = array();
                $sevenBootNumArr = array();
                $sevenOkOrderNumArr = array();
                $sevenOkOrderAmountArr = array();
                for($i=0; $i <= 23; $i++) {
                    $sevenBootClientNumArr[] = $val[$i]['bootClientNum'];
                    $sevenBootNumArr[] = $val[$i]['bootNum'];
                    $sevenOkOrderNumArr[] = $val[$i]['okOrderNum'];
                    $sevenOkOrderAmountArr[] = $val[$i]['okOrderAmount'];
                }
                
                $sevenData['bootClientNum'] = implode(', ', $sevenBootClientNumArr);
                $sevenData['bootNum'] = implode(', ', $sevenBootNumArr);
                $sevenData['okOrderNum'] = implode(', ', $sevenOkOrderNumArr);
                $sevenData['okOrderAmount'] = implode(', ', $sevenOkOrderAmountArr);
            
            }
            if ($key == 'thirty') {
                $thirtyBootClientNumArr = array();
                $thirtyBootNumArr = array();
                $thirtyOkOrderNumArr = array();
                $thirtyOkOrderAmountArr = array();
                for($i=0; $i <= 23; $i++) {
                    $thirtyBootClientNumArr[] = $val[$i]['bootClientNum'];
                    $thirtyBootNumArr[] = $val[$i]['bootNum'];
                    $thirtyOkOrderNumArr[] = $val[$i]['okOrderNum'];
                    $thirtyOkOrderAmountArr[] = $val[$i]['okOrderAmount'];
                }
                
                $thirtyData['bootClientNum'] = implode(', ', $thirtyBootClientNumArr);
                $thirtyData['bootNum'] = implode(', ', $thirtyBootNumArr);
                $thirtyData['okOrderNum'] = implode(', ', $thirtyOkOrderNumArr);
                $thirtyData['okOrderAmount'] = implode(', ', $thirtyOkOrderAmountArr);
            
            }
        }
        //处理今天的表格报表
        $totalArr = array('totalBootNum' => 0, 'totalBootClientNum' => 0, 'totalOkOrderNum' => 0, 'totalAmount' => 0,);
        $arrTm['today'] = empty($arrTm['today']) ? array() : $arrTm['today'];
        foreach ($arrTm['today'] as $item) {
            $totalArr['totalBootNum'] += $item['bootNum'];
            $totalArr['totalBootClientNum'] += $item['bootClientNum'];
            $totalArr['totalOkOrderNum'] += $item['okOrderNum'];
            $totalArr['totalAmount'] += $item['okOrderAmount'];
        }
        foreach ($arrTm['today'] as $key => $val) {
            $arrTm['today'][$key]['bootNumPer'] = ($totalArr['totalBootNum'] == 0) ? 0 : round($val['bootNum']/$totalArr['totalBootNum'], 4)*100;
            $arrTm['today'][$key]['clientPer'] = ($totalArr['totalBootClientNum'] == 0) ? 0 : round($val['bootClientNum']/$totalArr['totalBootClientNum'], 4)*100;
            $arrTm['today'][$key]['okOrderPer'] = ($totalArr['totalOkOrderNum'] == 0) ? 0 : round($val['okOrderNum']/$totalArr['totalOkOrderNum'], 4)*100;
            $arrTm['today'][$key]['amountPer'] = ($totalArr['totalAmount'] == 0) ? 0 : round($val['okOrderAmount']/$totalArr['totalAmount'], 4)*100;
        }
        $this->view->vars = $vars;
        $this->view->totalArr = $totalArr;
        $this->view->datatoday = empty($arrTm['today']) ? array() :sysSortArray($arrTm['today'],'hours',$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR");
        $this->view->todayData = $todayData;
        $this->view->yestodayData = $yestodayData;
        $this->view->sevenData = $sevenData;
        $this->view->thirtyData = $thirtyData;
    }
    
   
}